package com.kk.app.render;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.kk.app.RenderData;

import java.lang.ref.WeakReference;


/**
 * 测试的
 */
public class GLSurfaceRenderView extends SurfaceView {

    final String TAG = "__player_view__";
    private boolean mSurfaceCreated = false;
    private Object mSurfaceLock = new Object();
    private int mLastWidth = 0;
    private int mLastHeight = 0;
    private static long sKey = 0;
    private static Object sKeyObject = new Object();
    private long mKey = 0;

    private static long createKey() {
        synchronized (sKeyObject) {
            sKey++;
            return sKey;
        }
    }

    private SurfaceHolder.Callback mCallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            Log.i(TAG, "surfaceCreated: ");

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.i(TAG, "surfaceChanged: w:" + width + " h:" + height + "  format:" + format);
            synchronized (mSurfaceLock) {
                mLastWidth = width;
                mLastHeight = height;
                mSurfaceCreated = true;

                if (mKey == 0) {
                    mKey = createKey();
                }
                RenderData.getGlThreadManager().addSurface(holder.getSurface(), width, height, mKey);
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.i(TAG, "surfaceDestroyed: ");
            RenderData.getGlThreadManager().delSurface(mKey);
        }
    };

    public GLSurfaceRenderView(Context context) {
        this(context, null);
    }

    public GLSurfaceRenderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GLSurfaceRenderView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public GLSurfaceRenderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.getHolder().addCallback(mCallback);
    }


}
